﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
using Obsidian.Data.Sql;
using Obsidian.Edm;
using [#DalBaseNS#];
using [#ModelNS#];

/*============================
 * @author [#AUTHOR#]
 * @created [#CREATED#]
 * @desc 分类类型数据访问类
* ============================ */

namespace [#DalNS#]
{
    /// <summary>
    /// [#TbDescir#] 数据访问
    /// </summary>
    public class [#ClsName#]DA : TreeDA<[#ClsName#]Info>
    {
		
		#region [ADD]
		internal [#ClsName#]Info InsertCategory([#ClsName#]Info category)
        {
            return base.InsertNode(category);
        }
		#endregion
		
		#region [UPDATE]
		internal [#ClsName#]Info UpdateCategory([#ClsName#]Info category)
        {
            return base.UpdateNode(category);
        }
		#endregion
		
		#region [EXISTS]
		internal bool ExistsByName(string categoryName)
        {
            [#ClsName#]Info om = new [#ClsName#]Info();
            IDbHandler dbh = om.CreateHandler();
            dbh.SetField(om.CategoryId);
            dbh.Where(om.CategoryName.Equals(categoryName));

            DataReader dr = dbh.Select();
            return dr.ReadFirst<[#ClsName#]Info>() != null;
        }

        internal bool ExistsByName(long categoryId, string categoryName)
        {
            [#ClsName#]Info om = new [#ClsName#]Info();
            IDbHandler dbh = om.CreateHandler();
            dbh.SetField(om.CategoryId);
            dbh.Where(om.CategoryName.Equals(categoryName));
            dbh.Where(om.CategoryId.Compare("<>", categoryId));

            DataReader dr = dbh.Select();
            return dr.ReadFirst<[#ClsName#]Info>() != null;
        }
		#endregion
		
        #region [SELECT]
		internal [#ClsName#]Info SelectById(long categoryId)
        {
            [#ClsName#]Info om = new [#ClsName#]Info();
            IDbHandler dbh = om.CreateHandler();
            dbh.SetFields(om.Fields);
            dbh.Where(om.CategoryId.Equals(categoryId));
            DataReader dr = dbh.Select();
            return dr.ReadFirst<[#ClsName#]Info>();
        }
		
		internal [#ClsName#]Info SelectFirst([#ClsName#]Qry qry)
        {
            qry.IsPaging.Set(false);
            qry.PageSize.Set(1);
            List<[#ClsName#]Info> list = this.SelectCategoies(qry);
            return (list != null && list.Count > 0) ? list[0] : null;
        }

        internal List<[#ClsName#]Info> SelectCategories([#ClsName#]Qry qry)
        {
            [#ClsName#]Info om = new [#ClsName#]Info();
            IDbHandler dbh = om.CreateHandler();
            dbh.SetFields(om.Fields);

            DataReader dr = base.SelectList(dbh, om, qry);
            return dr.ReadList<[#ClsName#]Info>();
        }
        #endregion 
    }
}